<?php
class VehiclesController extends AppController {

	var $name = 'Vehicles';
        var $helpers = array('Ajax');
        var $components = array('RequestHandler');
	function index() {
                $this->set('title_for_layout', 'Configuración - Vehículos');
                $this->loadModel('Brand');        
		$this->Vehicle->recursive = 0;
		$this->set('vehicles', $this->paginate());
                $models=$this->Vehicle->Models->find('list',array('fields' => array('Models.descripcion')));
                $vehiclesTypes=$this->Vehicle->VehiclesTypes->find('list',array('fields' => array('VehiclesTypes.descripcion')));
                $brands=$this->Brand->find('list');              
                $this->set(compact('models', 'vehiclesTypes','brands'));
	}

	function view($id = null) {
            $this->set('title_for_layout', 'Configuración - Vehículos');
                $this->loadModel('Brand'); 
		if (!$id) {
			$this->Session->setFlash(__('Invalid vehicle', true));
			$this->redirect(array('action' => 'index'));
		}
                $models = $this->Vehicle->Models->find('list');
                $vehicle = $this->Vehicle->read(null, $id);
                $vehiclesTypes = $this->Vehicle->VehiclesTypes->find('list');
                $brands=$this->Brand->find('list');
		$this->set(compact('vehicle','models','VehiclesTypes','brands'));
	}

	function add() {
            $this->set('title_for_layout', 'Configuración - Vehículos');
             $this->loadModel('Brand'); 
		if (!empty($this->data)) {                   
                   
			$this->Vehicle->create();
			if ($this->Vehicle->save($this->data)) {
				$this->Session->setFlash(__('Vehiculo Guardado', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El vehicuo no se pudo guardar. por favor intente otra vez', true));
			}
		}
		$models = $this->Vehicle->Models->find('list',array('fields' => array('Models.descripcion')));
		$vehiclesTypes = $this->Vehicle->VehiclesTypes->find('list',array('fields' => array('VehiclesTypes.descripcion')));
		$brands=$this->Brand->find('list');
                $this->set(compact('models', 'vehiclesTypes','brands'));
	}
        function update_select(){
            
                if (!empty($this->data['Models']['brands_id']))
                {   
                        $marca_id = $this->data['Models']['brands_id'];
                 
                        $modelos = $this->Vehicle->Models->find('all', array('fields'=>array('Models.id','Models.descripcion'),'order'=>'id ASC','conditions'=>array('brands_id'=>$marca_id)));
                
                               
                }
                else
                {
                        $modelos = $this->Vehicle->Models->find('all', array('fields'=>array('Models.descripcion'),'order'=>'id ASC'));
                }
                $this->set('options', Set::combine($modelos, "{n}.Models.id","{n}.Models.descripcion"));
                $this->render('/vehicles/update_select', 'ajax');
        }
        
        function update_select_edit(){                 
                if (!empty($this->data['Vehicle']['brands_id']))
                {   
                    
                        $marca_id = $this->data['Vehicle']['brands_id'];
                 
                        $modelos = $this->Vehicle->Models->find('all', array('fields'=>array('Models.id','Models.descripcion'),'order'=>'id ASC','conditions'=>array('brands_id'=>$marca_id)));
                
                               
                }
                else
                {
                        $modelos = $this->Vehicle->Models->find('all', array('fields'=>array('Models.descripcion'),'order'=>'id ASC'));
                }
                $this->set('options', Set::combine($modelos, "{n}.Models.id","{n}.Models.descripcion"));
                $this->render('/vehicles/update_select', 'ajax');
        }

	function edit($id = null) {
            $this->set('title_for_layout', 'Configuración - Vehículos');
             $this->loadModel('Brand'); 
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid vehicle', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Vehicle->save($this->data)) {
				$this->Session->setFlash(__('The vehicle has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The vehicle could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Vehicle->read(null, $id);
                        
		}
                
                $brands=$this->Brand->find('list');

           
                $marcaid = $this->Brand->find('all',
                                                    array('conditions'=>array('id'=>$this->data['Models']['brands_id']),
                                                           'fields'=>array('id','descripcion')
                                                        )
                                               );
               
                $models = $this->Vehicle->Models->find('list',array('conditions'=>array('brands_id'=>$marcaid['0']['Brand']['id']),
                                                           'fields'=>array('id','descripcion')
                                                        ));
                
                $vehiculo = $this->data['Vehicle'];
                
		$vehiclesTypes = $this->Vehicle->VehiclesTypes->find('list',array('fields'=> array('VehiclesTypes.descripcion')));
		$this->set(compact('models', 'vehiclesTypes','brands','vehiculo','marcaid'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for vehicle', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Vehicle->delete($id)) {
			$this->Session->setFlash(__('Vehicle deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Vehicle was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
